package com.example.framework2002a.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.framework2002a.R;
import com.example.framework2002a.adapter.HeadLinesAdapter;
import com.example.framework2002a.databinding.FragmentHeadlinesBinding;
import com.example.lib_core.bean.HeadlinesBean;
import com.example.lib_core.factory.FragFactory;
import com.example.lib_core.factory.FragViewModel;
import com.fhz.utilsfhz.ApiResponse;

import java.util.ArrayList;
import java.util.List;

public class HeadlinesFragment extends Fragment {

    private FragmentHeadlinesBinding binding;
    private FragViewModel fragViewModel;
    private List<HeadlinesBean> headlinesBeans = new ArrayList<>();
    private HeadLinesAdapter headLinesAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_headlines,container,false);

        fragViewModel = new ViewModelProvider(this,new FragFactory()).get(FragViewModel.class);
        fragViewModel.getHeadlines(2,2,10).observe(this, new Observer<ApiResponse<List<HeadlinesBean>>>() {
            @Override
            public void onChanged(ApiResponse<List<HeadlinesBean>> listApiResponse) {
                headlinesBeans.addAll(listApiResponse.getData());

                headLinesAdapter = new HeadLinesAdapter(headlinesBeans,binding);
                binding.headlinesRv.setAdapter(headLinesAdapter);
                binding.headlinesRv.setLayoutManager(new LinearLayoutManager(getContext()));
            }
        });

        return binding.getRoot();
    }
}